package com.lvmama.rhino.service.impl.comment;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lvmama.rhino.mapperComment.CommentDetailMapper;
import com.lvmama.rhino.mapperComment.ViewDetailMapper;
import com.lvmama.rhino.pojo.comment.Enum.EnumForCategory;
import com.lvmama.rhino.pojo.comment.Enum.EnumForComment;
import com.lvmama.rhino.pojo.comment.entity.CommentDetail;
import com.lvmama.rhino.pojo.comment.entity.ViewDetail;
import com.lvmama.rhino.pojo.comment.request.CommentListRequest;
import com.lvmama.rhino.pojo.comment.request.ViewDetailRequest;
import com.lvmama.rhino.service.comment.CommentListService;
import com.lvmama.rhino.utils.Constants;
import com.lvmama.rhino.utils.StringTools;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 获取点评列表
 */

@Service
@Transactional
public class CommentListServiceImpl implements CommentListService {

    @Autowired
    private CommentDetailMapper commentDetailMapper;

    @Autowired
    private ViewDetailMapper viewDetailMapper;

    @Override
    public PageInfo<CommentDetail> getCommentList(CommentListRequest request) throws Exception {

        PageHelper.startPage(request.getCurrentPage(), request.getPageSize());
        PageInfo<CommentDetail> commentDetalList = new PageInfo<>(commentDetailMapper.getCommentDetalList(request));
        // 枚举转换&评论内容过长只记录130个字符
        transCommentEnum(commentDetalList);
        return commentDetalList;
    }

    /**
     * 点评列表枚举转换及数据处理
     *
     * @param commentDetalList
     */
    private void transCommentEnum(PageInfo<CommentDetail> commentDetalList) throws Exception {
        List<CommentDetail> commentDetails = commentDetalList.getList();
        if (!CollectionUtils.isEmpty(commentDetails)) {
            for (int i = 0; i < commentDetails.size(); i++) {
                CommentDetail commentDetail = commentDetails.get(i);
                // 情感极性
                String senseTypeStr = commentDetail.getSenseType();
                if (StringUtils.isNotBlank(senseTypeStr)) {
                    int senseType = Integer.parseInt(senseTypeStr);
                    String senseTypeName = senseType > -1 ? EnumForComment.getCommentShowname(senseType) : "";
                    commentDetail.setSenseTypeName(senseTypeName);
                }

                // 品类名称
                String categoryId = commentDetail.getCategoryId();
                String categoryName = Constants.UNKNOWN;
                if(!Constants.UNKNOWNDATA.equals(categoryId)) {
                    categoryName = StringUtils.isNotBlank(categoryId) ? EnumForCategory.getCategoryShowName(categoryId) : null;
                    if(StringUtils.isBlank(categoryName)) {
                        categoryName = EnumForCategory.getCategoryName(categoryId);
                    }
                }
                commentDetail.setCategoryName(categoryName);

                // 产品名称
                String productName = commentDetail.getProductName();
                if (StringUtils.isBlank(productName)) {
                    productName = Constants.UNKNOWN;
                } else if (Constants.UNKNOWNDATA.equals(productName.trim()) ||
                        Constants.UNKNOWNDATA2.equals(productName.trim()) ||
                        Constants.UNKNOWNDATA3.equalsIgnoreCase(productName.trim())) {
                    productName = Constants.UNKNOWN;
                }
                commentDetail.setProductName(productName);

                String content = commentDetail.getContent();
                int contentShortLength = 130; // 评论最大显示字符数
                // 评论中有html标签
                if (StringUtils.isNotBlank(content) && content.length() > contentShortLength) {
                    if (StringTools.isContainsLabel(content)) {
                        String con = StringTools.filterHtml(content);
                        int size = contentShortLength > con.length() ? con.length() : contentShortLength;
                        commentDetail.setContentShort(con.substring(0, size) + "...");
                    } else {
                        commentDetail.setContentShort(content.substring(0, contentShortLength) + "...");
                    }
                }
            }
        }
    }

    @Override
    public List<CommentDetail> getAllCommentList() {
        // TODO Auto-generated method stub
        return commentDetailMapper.queryAllCommentList();
    }

    @Override
    public List<ViewDetail> getViewPoint(ViewDetailRequest request) {

        List<ViewDetail> viewDetailList = viewDetailMapper.getViewPoint(request);
        return viewDetailList;
    }

}
